import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, Refresher} from 'ionic-angular';
import {AppUrl} from "../../../app/app.general";
import {PageSplit} from "../../utils/page/page";
import {HttpClient} from "@angular/common/http";
import {AppConfig} from "../../../app/appConfig";

/**
 * Generated class for the BusinessFoodOrderPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-business-food-order',
  templateUrl: 'business-food-order.html',
})
export class BusinessFoodOrderPage {

  @ViewChild(Content) content: Content;
  @ViewChild(Refresher) refresher: Refresher;
  private page: PageSplit = {
    pageSize: 10,
    pageNo: 0,
    isLastPage: false
  };

  // orderList=[

    // { floorNo:'一楼',
    //   tableNo:'一号桌',
    //   tableOrder:[
    //     {food:'米饭',num:4},{food:'小炒肉',num:1},
    //     {food:'黄焖鸡',num:1},{food:'红酒排骨',num:1},
    //     {food:'米饭',num:4},{food:'小炒肉',num:1},
    //     {food:'黄焖鸡',num:1},{food:'红酒排骨',num:1}
    //     ],
    //   printStat:'已完成'
    //   ,printTime:'2018-01-01 12:32:10'},
    // { floorNo:'一楼',
    //   tableNo:'二号桌特使',
    //   tableOrder:[
    //     {food:'米饭',num:4},{food:'小炒肉',num:1},
    //     {food:'黄焖鸡',num:1},{food:'红酒排骨',num:1},
    //     {food:'米饭',num:4},{food:'小炒肉',num:1},
    //     {food:'黄焖鸡',num:1},{food:'红酒排骨',num:1}
    //   ],
    //   printStat:'已完成'
    //   ,printTime:'2018-01-01 12:32:10'}
    // ];

  constructor(public navCtrl: NavController, public navParams: NavParams,private http:HttpClient,private appConfig: AppConfig) {
  this.orderList = [];
  }

  orderList:Array<{
    orderno:string,
    trandate:string,
    tableno:string,
    totalprice:any,
     payTypeName:string,
     discountrate:any,
     realpay :any,
     statusName :string,
     tableName:string,
     floorName:string,
    orderDetail:Array<{foodname:string,num:any,del:any}>}>;

  getData(refresher?:any){
    this.page.pageNo++;

    this.http.post(AppUrl.food.foodOrderByRId,{pageNo:this.page.pageNo,pageSize:this.page.pageSize}).subscribe(data=>{
      if(refresher){
        this.refresher.complete();
        this.orderList=[];
      }

      console.log(data);
      let temp1:any=data;
      let temp:any=temp1.data;
      for(let ar of temp[0]){
        let index=this.orderList.findIndex(e=>e.orderno==ar.orderno);
        if(index!=-1){
          this.orderList[index].orderDetail.push({
            foodname:ar.foodname,
            num:ar.num,
            del:ar.del
          })
        }else{
          this.orderList.push({
            orderno:ar.orderno,
                          trandate:ar.trandate,
                          tableno:ar.tableno,
                          totalprice:ar.totalprice,
                          payTypeName:ar.payTypeName,
                          discountrate:ar.discountrate,
                          realpay : ar.realpay,
                          statusName :ar.statusName,
                           tableName:ar.tableName,
                           floorName:ar.floorName,
            orderDetail:[{
              foodname:ar.foodname,
              num:ar.num,
              del:ar.del
            }]
          });
        }
      }
      this.page.isLastPage = temp[1]==0?true:false;
    });
  }
  // getData(refresher?: any) {
  //   this.page.pageNo++;
  //   this.http.post(AppUrl.food.foodOrderByRId,{restId: localStorage.getItem('restaurantid'),pageNo:this.page.pageNo,pageSize:this.page.pageSize}).subscribe(data => {
  //     if(refresher){
  //       this.refresher.complete();
  //       this.orderList = [];
  //     }
  //     let temp:any = data;
  //     if (temp.code === 200) {
  //       // this.orderList = [];
  //       for(let order of temp.data.list){
  //         this.http.post(AppUrl.food.foodOrderDetailById,{orderId: order.orderno}).subscribe(data => {
  //           let tmp:any = data;
  //           this.orderList.push({
  //               orderno:order.orderno,
  //               trandate:order.trandate,
  //               tableno:order.tableno,
  //               totalprice:order.totalprice,
  //               payTypeName:order.payTypeName,
  //               discountrate:order.discountrate,
  //               realpay : order.realpay,
  //               statusName :order.statusName,
  //                tableName:order.tableName,
  //                floorName:order.floorName,
  //                orderDetail: tmp.data,
  //           });
  //         });
  //       }
  //       // tmpList = this.menuList.concat(temp.data.list);
  //       this.page.isLastPage = temp.data.isLastPage;
  //     }
  //     else {
  //       this.appConfig.presentToast(temp.msg);
  //
  //     }
  //   });
  // }

  doInfinite(infiniteScroll): Promise<any> {
    return new Promise(resolve => {
      let that = this;
      setTimeout(function () {
        if (!that.page.isLastPage) {
          that.getData();
          resolve();
        }
        else {
          resolve();
        }
      }, 1000);
    });
  }

  doRefresh() {
    this.page = {
      isLastPage: false,
      pageSize: 10,
      pageNo: 0
    };
    this.getData(this.refresher);
  }

  ionViewWillEnter(){

    this.refresher._top = '50px';
    this.refresher.state = 'refreshing';
    // this.refresher._beginRefresh();
    this.doRefresh();
    // this.getData();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BusinessFoodOrderPage');
  }

  cashDetail(orderId,tableName,totalprice) {
    let order :any ={tableid: '', tablename: tableName,sumPrice:totalprice,orderno:orderId}

    // this.navCtrl.push('CashdetailPage', {orderno:orderId});
    this.navCtrl.push('CashdetailPage', {data:order,floor:'',pagetitle:'收银'});
  }

}
